package com.zsqat.eshop.member.controller;

import java.util.Arrays;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zsqat.common.enums.BizCodeEnum;
import com.zsqat.eshop.member.exception.PhoneException;
import com.zsqat.eshop.member.exception.UserNameException;
import com.zsqat.eshop.member.feign.CouponFeignService;
import com.zsqat.eshop.member.vo.MemberUserLoginVo;
import com.zsqat.eshop.member.vo.MemberUserRegisterVo;
import com.zsqat.eshop.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zsqat.eshop.member.entity.MemberEntity;
import com.zsqat.eshop.member.service.MemberService;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.R;


/**
 * 会员
 *
 * @author zsqat
 * @email 491192853@qq.com
 * @date 2020-11-19 15:35:47
 */
@RestController
@RequestMapping("member/member" )
public class MemberController {
  @Autowired
  private MemberService memberService;

  // 测试远程调用
  @Autowired
  CouponFeignService couponFeignService;

  @RequestMapping("/coupons" )
  public R test() {
    MemberEntity memberEntity = new MemberEntity();
    memberEntity.setNickname("张三" );
    R membercoupons = couponFeignService.membercoupons(); // 假设张三去数据库查了后返回了张三的优惠券信息

    //打印会员和优惠券信息
    return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons" ));
  }


  /**
   * 列表
   */
  @RequestMapping("/list" )
  // @RequiresPermissions("member:member:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = memberService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{id}" )
  // @RequiresPermissions("member:member:info")
  public R info(@PathVariable("id" ) Long id) {
    MemberEntity member = memberService.getById(id);

    return R.ok().put("member", member);
  }

  /**
   * 保存
   */
  @RequestMapping("/save" )
  // @RequiresPermissions("member:member:save")
  public R save(@RequestBody MemberEntity member) {
    memberService.save(member);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update" )
  // @RequiresPermissions("member:member:update")
  public R update(@RequestBody MemberEntity member) {
    memberService.updateById(member);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete" )
  // @RequiresPermissions("member:member:delete")
  public R delete(@RequestBody Long[] ids) {
    memberService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

  @PostMapping(value = "/register")
  public R register(@RequestBody MemberUserRegisterVo memberUserRegisterVo) {

    try {
      memberService.register(memberUserRegisterVo);
    } catch (PhoneException ex) {
      return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
    } catch (UserNameException ex) {
      return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
    }

    return R.ok();
  }

  @PostMapping(value = "/login")
  public R login(@RequestBody MemberUserLoginVo memberUserLoginVo) {

    MemberEntity memberEntity = memberService.login(memberUserLoginVo);

    if (memberEntity != null) {
      return R.ok().setData(memberEntity);
    } else {
      return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
    }
  }

  @PostMapping(value = "/oauth2/login")
  public R oauthLogin(@RequestBody SocialUser socialUser) throws Exception {

    MemberEntity memberEntity = memberService.login(socialUser);

    if (memberEntity != null) {
      return R.ok().setData(memberEntity);
    } else {
      return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
    }
  }

  @PostMapping(value = "/weixin/login")
  public R weixinLogin(@RequestParam("accessTokenInfo") String accessTokenInfo) {

    MemberEntity memberEntity = memberService.login(accessTokenInfo);
    if (memberEntity != null) {
      return R.ok().setData(memberEntity);
    } else {
      return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
    }
  }
}
